package mess;

public class LeeCode_83 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1,new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(3)))));
        ListNode l2 = new ListNode(1,new ListNode(2,new ListNode(2,new ListNode(2))));
        ListNode l3 = new ListNode(1,new ListNode(1,new ListNode(1,new ListNode(1))));
        //System.out.println(deleteDuplicates(l1));
        //System.out.println(deleteDuplicates(l2));
        System.out.println(deleteDuplicates(l2));
    }

    private static ListNode deleteDuplicates(ListNode head) {
        ListNode index = head;
        boolean flag = true;
        while (index != null && index.next != null){
            if (index.val == index.next.val){
                if (flag){
                    index = index.next;
                    head = index;
                    continue;
                }
                index.next = index.next.next;
                continue;
            }
            index = index.next;
            flag = false;
        }
        return head;
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return this.val + "->" + next;
        }
    }
}
